const mongoose = require('mongoose')

mongoose.connect('mongodb://localhost/xia')
.then(()=> {
    console.log('连接成功...')
    UserInfo.find({}).then(res => {
        console.log(res)
    })
    .catch(err => {
        console.log(err)
    })
})
.catch(err => {
    console.log(err)
})

// 添加数据
const UserInfo = mongoose.model('UserInfo', new mongoose.Schema({
    name: {
        type: String,
        minlength: [2, '用户名不能小于2个字符'],
        maxlength: 20,
    },
    age: {
        type: Number, 
        min: 2,
        max: [50, '年龄不能超过50岁']
    },
    hobbies: [String],
    email: String,

    
}))
// 添加数据
UserInfo.create({name:'夏天', age: 27, hobbies: ['学习', '看电影'], email: '000@qq.com'}).then(() => {
    console.log('添加成功')

})
.catch(err => {
            Object.keys(err.errors).forEach(item => {
            console.log(err.errors[item].properties.message)
        })
})
